package com.plum.admin.modules.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.google.gson.Gson;
import com.plum.admin.common.constant.Constant;
import com.plum.admin.common.enums.EmailEnum;
import com.plum.admin.common.util.*;
import com.plum.admin.common.vo.EmailValidate;
import com.plum.admin.modules.sys.entity.SysUser;
import com.plum.admin.modules.sys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/9/14 16:19
 */
@RestController
@Api(tags = "发送邮件API")
@RequestMapping("/email")
public class EmailController {
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IpUtil ipUtil;
    @Autowired
    private RedisCache redisCache;
    @ApiOperation("发送重置密码邮件验证码")
    @GetMapping("/sendResetCode/{email}")
    public R<Object> sendResetCode(@PathVariable String email,HttpServletRequest request){
        return sendEmail(email,EmailEnum.RESET_EMAIL,"发送重置密码邮件","code-template",request);
    }
    @ApiOperation("发送修改邮件验证码")
    @GetMapping("/sendEditEmailCode/{email}")
    public R<Object> sendEditEmailCode(@PathVariable String email,HttpServletRequest request){
        return sendEmail(email,EmailEnum.EDIT_EMAIL,"发送修改密码邮件","code-template",request);
    }
    public R<Object> sendEmail(String email, EmailEnum emailEnum, String title, String template, HttpServletRequest request){
        EmailValidate e=new EmailValidate();
        e.setOperation(emailEnum.getOperation());
        SysUser user = userService.findByEmail(email);
        if (emailEnum.getType().equals(EmailEnum.EDIT_EMAIL.getType())) {
            if (user != null) {
                return R.error("该邮箱已绑定账号");
            }
            SysUser u = securityUtil.getUser();
            e.setUsername(u.getUsername());
        } else if (emailEnum.getType().equals(EmailEnum.RESET_EMAIL.getType())) {
            if (user == null) {
                return R.error("该邮箱未注册");
            }
            e.setUsername(user.getUsername());
        }
        String key ="sendEmailCode:"+ipUtil.getIpAddr(request);
        Object object = redisCache.getCacheObject(key);
        if(ObjectUtil.isNotNull(object)){
            return R.error("您发送的太频繁了");
        }
        String code = RandomUtil.randomNumbers(6);
        e.setCode(code);
        e.setEmail(email);
        e.setFullUrl("https://www.baidu.com");
        redisCache.setCacheObject(Constant.PRE_EMAIL+email,new Gson().toJson(e),2L, TimeUnit.MINUTES);
        emailUtil.sendTemplateEmail(email, title, template, e);
        redisCache.setCacheObject(key,"emailSend",1L,TimeUnit.MINUTES);
        return R.ok("发送成功");
    }
}
